package org.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.io.DataInputStream;

/**
 * 读取文件内容
 */
public class Demo03ReadFile {
    @Test
    public void testReadFile() throws Exception{
        String server = "hdfs://192.168.56.201:8020";
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        System.setProperty("HADOOP_HOME", "D:/program/hadoop-3.2.2");
        System.setProperty("hadoop.home.dir", "D:/program/hadoop-3.2.2");
        Configuration config = new Configuration();
        config.set("fs.defaultFS", server);
        config.set("HADOOP_HOME", "/app/hadoop-3.2.2");
        try (FileSystem fs = FileSystem.get(config)) {
            DataInputStream in =  fs.open(new Path(server+"/test/a.txt"));
            int len = 0;
            byte[] bs = new byte[1024];
            while((len=in.read(bs))!=-1){
                String str = new String(bs,0,len);
                System.out.print(str);
            }
        }
    }
}
